package com.cloudatum.service;

import com.cloudatum.redis.RedisUtil;
import com.cloudatum.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 活动服务层
 * Created by Administrator on 2015/11/1.
 */
@Service
public class ActivityService {

    static Logger logger = LoggerFactory.getLogger(ActivityService.class);

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityListService activityListService;

    public Map currentActivity(){
        Jedis jedis = null;
        Map<String,String> jedisMap = null;
        try {
            jedis = RedisUtil.getJedis();
            //添加页面访问量
            jedis.incr(Constants.COUNT_INDEX_CURRENT);
            jedisMap = jedis.hgetAll(Constants.CURRENT_MIAOSHA);
            if (jedisMap.isEmpty()){
                logger.debug("切换秒杀活动！！！");
                jedis.watch(Constants.CURRENT_MIAOSHA);
                Map map  =  activityMapper.selectCurrentActivity();
                if (null != map){
                    String activityId = map.get("id").toString();
                    long beginTime = ((Date)map.get("beginTime")).getTime();
                    int goodsId = (int)map.get("goodsId");
                    long endTime = ((Date)map.get("endTime")).getTime();
                    String regionLimit = map.get("regionLimit").toString();
                    int peopleLimit = (int)map.get("peopleLimit");
                    String goodsName = map.get("goodsName").toString();
                    int realStock = (int)map.get("realStock");
                    int stock = (int)map.get("stock");
                    String goodsImg = map.get("goodsImg").toString();
                    String activityPrice = map.get("activityPrice").toString();
                    String realPrice = map.get("realPrice").toString();

                    jedisMap.put(Constants.CURRENT_MIAOSHA_ID,activityId);
                    jedisMap.put(Constants.BEGIN_TIME,beginTime+"");
                    jedisMap.put(Constants.CURRENT_GOODS_ID,goodsId+"");
                    jedisMap.put(Constants.END_TIME,endTime+"");
                    jedisMap.put(Constants.REGION_LIMIT,regionLimit);
                    jedisMap.put(Constants.PEOPLE_LIMIT,peopleLimit+"");
                    jedisMap.put(Constants.CURRENT_GOODS_NAME,goodsName);
                    jedisMap.put(Constants.CURRENT_GOODS_REALSTOCK,realStock+"");
                    jedisMap.put(Constants.CURRENT_GOODS_STOCK,stock+"");
                    jedisMap.put(Constants.CURRENT_GOODS_IMG,goodsImg);
                    jedisMap.put(Constants.CURRENT_ACTIVITY_PRICE,activityPrice);
                    jedisMap.put(Constants.CURRENT_REAL_PRICE, realPrice);

                    //watch锁
                    Transaction tx = jedis.multi();
                    tx.hmset(Constants.CURRENT_MIAOSHA,jedisMap);
                    tx.expireAt(Constants.CURRENT_MIAOSHA,endTime/1000);
                    List<Object> resp = tx.exec();
                    if (null == resp){
                        logger.debug("其他事务已更新活动缓存，不需要重复更新！！");
                    } else {
                        logger.debug("==>向app推送当前，或者将要进行的活动内容");
                        activityListService.sendNextActivity();
                    }
                }else {
                    jedis.unwatch();
                    jedisMap.put("status","0");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            RedisUtil.returnResource(jedis);
        }
        if (null != jedisMap) {
            String beginTimeStr = jedisMap.get(Constants.BEGIN_TIME);
            if (null != beginTimeStr) {
                long beginTime = Long.parseLong(beginTimeStr);
                if (System.currentTimeMillis() > beginTime){
                    jedisMap.put(Constants.CURRENT_GOODS_STOCK, jedisMap.get(Constants.CURRENT_GOODS_REALSTOCK));
                }
                jedisMap.put("leftMillis", beginTime-System.currentTimeMillis()+"");
            }

            jedisMap.remove(Constants.CURRENT_GOODS_REALSTOCK);
            jedisMap.remove(Constants.PEOPLE_LIMIT);
        }
        return jedisMap;
    }

}
